package com.example.smartall.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.entity.HospitalCardBean;
import com.example.smartall.ui.CreatePatientCardActivity;
import com.example.smartall.ui.HospitalCategoryActivity;

import java.util.List;

/**
 * @author abbas
 * @brief description
 * @date 2022-01-27
 */
public class HospitalCardRecycleAdapter extends RecyclerView.Adapter<HospitalCardRecycleAdapter.ViewHolder> {

    private Context context;
    private List<HospitalCardBean.RowsBean> bean;
    private OnClickListener listener;

    public HospitalCardRecycleAdapter(Context context, List<HospitalCardBean.RowsBean> bean) {
        this.context = context;
        this.bean = bean;
    }

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.card_item,null,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Glide.with(context).load(ApiConfig.BASE_URL + bean.get(position).getImgUrl()).into(holder.avater);
        holder.name.setText(bean.get(position).getName());
        if (position == 0){
            holder.tip.setText("未完善");
            holder.ibIn.setVisibility(View.INVISIBLE);
        }else {
            holder.tip.setText("修改此就诊卡信息");
            holder.ibIn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //TODO 跳转至门诊科室分诊页面 name属性开始了漂泊
                    Intent intent = new Intent(context,HospitalCategoryActivity.class);
                    intent.putExtra("cardName",bean.get(position).getName());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return bean.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView avater;
        private TextView name;
        private TextView tip;
        private ImageButton ibIn;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            avater = (ImageView) itemView.findViewById(R.id.avater);
            name = (TextView) itemView.findViewById(R.id.name);
            tip = (TextView) itemView.findViewById(R.id.tip);
            ibIn = (ImageButton) itemView.findViewById(R.id.ib_in);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null){
                        listener.onClicked(getAdapterPosition());
                    }
                }
            });

        }
    }

    public interface OnClickListener{
        void onClicked(int position);
    }
}

